package com.future.hedongjun.home.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.future.baselib.entity.BaseResponse;
import com.future.baselib.http.HttpRequest;
import com.future.hedongjun.R;
import com.future.hedongjun.common.MyApp;
import com.future.hedongjun.home.adapter.ClassCoursewareAdapter;
import com.future.hedongjun.home.utils.OnItemClickListener;
import com.future.hedongjun.home.bean.ClassCoursewareBean;
import com.future.hedongjun.home.bean.response.ClassCoursewareResponse;
import com.future.hedongjun.mine.ui.LoginActivity;
import com.kingsoft.media.httpcache.OnCacheStatusListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 创建日期：2018/11/19 17:57
 * author liu.p.f
 * 类说明： 课件
 */
public class ClassCoursewareFragment extends Fragment implements OnItemClickListener {


    @BindView(R.id.recycler_classHour)
    RecyclerView recyclerClassHour;
    private ArrayList<ClassCoursewareBean> beans;
    private String ClassId;
    private List<ClassCoursewareBean> classHourBeans;
    Unbinder unbinder;
    private ClassCoursewareAdapter classCoursewareAdapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_courseware, container, false);
        unbinder = ButterKnife.bind(this, view);
        getData();
        return view;
    }


    private void getData() {
        if (MyApp.getInstance().getUserToken().isEmpty()) {
            startActivity(new Intent(getContext(), LoginActivity.class));
        } else {
            new HttpRequest<ClassCoursewareResponse>()
                    .with(getContext())
                    .setApiCode("_classdetail_001")
                    .addParam("id", ClassId)
                    .addParam("userToken", MyApp.getInstance().getUserToken())
                    .addSuccessListener(new HttpRequest.OnNetworkSuccess() {
                        @Override
                        public void onSuccess(BaseResponse response) {
                            classHourBeans = ((ClassCoursewareResponse) response).data;
                            initView();
                        }
                    }).start(new ClassCoursewareResponse());
        }
    }

    private void initView() {
        classCoursewareAdapter = new ClassCoursewareAdapter(getContext(), classHourBeans);
        recyclerClassHour.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerClassHour.setAdapter(classCoursewareAdapter);
        classCoursewareAdapter.setItemClickListener(this);
    }


    private OnCacheStatusListener mCacheStatusListener = new OnCacheStatusListener() {
        @Override
        public void OnCacheStatus(String url, long sourceLength, int percentsAvailable) {
//            if (percentsAvailable == 100) {
//                //rvProgress.setText("已缓存");
//            } else {
//                //rvProgress.setText(percentsAvailable+"%");
//            }
        }
    };

//    private OnErrorListener mOnErrorListener = new OnErrorListener() {
//        @Override
//        public void OnError(int errCode) {
//            Toast.makeText(mContext, "HttpCache meet an error:" + errCode, Toast.LENGTH_SHORT).show();
//        }
//    };

    public void setClassId(String classId) {
        this.ClassId = classId;
    }

    @Override
    public void onItemClick(View v) {
        //KSYProxyService mProxyService = MyApp.getKSYProxy(mContext);
//        mProxyService.registerErrorListener(mOnErrorListener);
//        mProxyService.setMaxFilesCount(500);
//        mProxyService.setCacheRoot(new File(Environment.getExternalStorageDirectory().toString() + File.separator + "CacheFile" + "/" + "CacheVideo" + "/" + beans.get(position).courseName));
//        mProxyService.startServer();
//        mProxyService.startPreDownload(item.address);
//        mProxyService.registerCacheStatusListener(mCacheStatusListener,item.address);
    }
}
